home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Devices / CD-ROM / How to Detect a CD / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-18  |  4.6 KB  |  205 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include <types.h>
  3. #include <Files.h>
  4. #include <Devices.h>
  5.  
  6. #include "WhereCDs.h"
  7.  
  8. /* 
  9.  * use this version of a Control structure for a ReadTOC call
  10.  */
  11. typedef struct {
  12.     struct QElem *qLink;
  13.     short qType;
  14.     short ioTrap;
  15.     Ptr ioCmdAddr;
  16.     ProcPtr ioCompletion;
  17.     OSErr ioResult;
  18.     char *ioNamePtr;
  19.     short ioVRefNum;
  20.     short ioCRefNum;
  21.     short csCode;
  22.     unsigned long    discID;
  23.     char    unused[18];
  24. } CDTOCParam;
  25.  
  26. enum {
  27.     kReadTOC = 100,
  28.     kDiskIsLocked = 0x80,
  29.     kDiskEjectableFlag = 0x08
  30. };
  31.  
  32. #define kDriverName    "\p.AppleCD"
  33.  
  34. void main(void);
  35. Boolean DriveIsEjectable(DrvQEl *queueElementPtr);
  36. Boolean DriveIsHardwareLocked(DrvQEl *queueElementPtr);
  37. OSErr IDDisc(short refNum, unsigned long *discID);
  38. void TechNoteCDs(void);
  39.  
  40.  
  41. /************************************************************************
  42.  *
  43.  *  Function:    main
  44.  *    
  45.  *  Purpose:    for testing only
  46.  *
  47.  *  Returns:    nothing
  48.  *
  49.  *  Side Effects: prints list of available CD-ROM drives (best guess)
  50.  *
  51.  *  Description: scan drive queue.  Call PBControl with a csCode 100
  52.  *                 ReadTOC call. See tech note DV 22 for details.
  53.  *
  54.  ************************************************************************/
  55. void main(void)
  56. {
  57.     OSErr    err;
  58.     short    refNum;
  59.     UInt32    discID = 0L;
  60.     QHdrPtr    driveQueuePtr;
  61.     DrvQEl    *queueElementPtr;
  62.  
  63.     printf("Test of identifying a disc\n");    // initializes toolbox
  64.  
  65.     driveQueuePtr = GetDrvQHdr();
  66.     queueElementPtr = (DrvQEl *)driveQueuePtr -> qHead;
  67.  
  68.     do 
  69.     {
  70.         refNum = queueElementPtr->dQRefNum;
  71.         
  72.         if ( DriveIsEjectable(queueElementPtr) && 
  73.                  DriveIsHardwareLocked(queueElementPtr) )
  74.             printf("drive with refNum %d (0x%x) is ejectable and hardware locked\n", 
  75.                         refNum, refNum);
  76.         
  77.         err = IDDisc(refNum, &discID);        // If we don't get an error from
  78.                                             // calling ReadTOC, assume it's a CD.
  79.         if (!err)
  80.             printf("I think this is a CD-ROM disc.\n");
  81.         
  82.  
  83.         queueElementPtr = (DrvQEl *)queueElementPtr->qLink;
  84.     } while (queueElementPtr != nil);
  85.  
  86.     TechNoteCDs();
  87.     printf("Finished!\n");
  88. }
  89.  
  90.  
  91. /************************************************************************
  92.  *
  93.  *  Function:        DriveIsEjectable
  94.  *
  95.  *  Purpose:        determine if drive is ejectable
  96.  *
  97.  *  Returns:        true if ejectable, false if not
  98.  *
  99.  *  Side Effects:    none
  100.  *
  101.  *  Description:    look at the 4 bytes preceeding the drive queue
  102.  *                    element.
  103.  *
  104.  ************************************************************************/
  105. Boolean DriveIsEjectable(DrvQEl *queueElementPtr)
  106. {
  107.     UInt8 *    bytePtr;
  108.     
  109.     bytePtr = (UInt8 *)queueElementPtr;
  110.     bytePtr -= 4;
  111.     return ( (bytePtr[1] & kDiskEjectableFlag) != kDiskEjectableFlag );
  112.  
  113. }
  114.  
  115.  
  116. /************************************************************************
  117.  *
  118.  *  Function:        DriveIsHardwareLocked
  119.  *
  120.  *  Purpose:        determine if drive is HardwareLocked
  121.  *
  122.  *  Returns:        true if HardwareLocked, false if not
  123.  *
  124.  *  Side Effects:    none
  125.  *
  126.  *  Description:    look at the 4 bytes preceeding the drive queue
  127.  *                    element.
  128.  *
  129.  ************************************************************************/
  130. Boolean DriveIsHardwareLocked(DrvQEl *queueElementPtr)
  131. {
  132.     UInt8 *    bytePtr;
  133.     
  134.     bytePtr = (UInt8 *)queueElementPtr;
  135.     bytePtr -= 4;
  136.     return ( (bytePtr[0] & kDiskIsLocked) == kDiskIsLocked );
  137.  
  138. }
  139.  
  140.  
  141. /************************************************************************
  142.  *
  143.  *  Function:        IDDisc
  144.  *
  145.  *  Purpose:        return total time on this disc as unique ID
  146.  *
  147.  *  Returns:        OSErr
  148.  *                    either noErr if everything was okay
  149.  *                    or some parameter error from driver call.
  150.  *
  151.  *  Side Effects:    fills in discID
  152.  *
  153.  *  Description:    call the driver ReadTOC call to get lead-out time.
  154.  *                    Return this time as the unique id for this disc.
  155.  *
  156.  ************************************************************************/
  157. OSErr IDDisc(short refNum, unsigned long *discID)
  158. {
  159.     CDTOCParam    myPB;
  160.     OSErr    result;
  161.     
  162.     myPB.ioCompletion = 0;
  163.     myPB.ioNamePtr = (char *) 0;
  164.     myPB.ioVRefNum = 0;
  165.     myPB.ioCRefNum = refNum;
  166.     myPB.csCode = kReadTOC;
  167.     myPB.discID = 0x00020000;    /* request lead-out time */
  168.     
  169.     result = PBControlSync((ParmBlkPtr)&myPB);
  170.     
  171.     if (result == noErr)
  172.         *discID = myPB.discID >> 8;
  173.     else
  174.         *discID = 0;
  175.     return result;
  176. }
  177.  
  178.  
  179. /************************************************************************
  180.  *
  181.  *  Function:        TechNoteCDs    
  182.  *
  183.  *  Purpose:        use the technique in DV 22 to find CD-ROM drives
  184.  *
  185.  *  Returns:        nothing
  186.  *
  187.  *  Side Effects:    prints a list of found CDs
  188.  *
  189.  *  Description:    call the routine documented in DV 22 and print 
  190.  *                    the result
  191.  *
  192.  ************************************************************************/
  193. void TechNoteCDs(void)
  194. {
  195.     UInt8    locations;
  196.     short    i;
  197.     
  198.     locations = WhereCDs();
  199.     printf("The WhereCDs() procedure reports there are CDs at SCSI IDs: ");
  200.     for (i = 0; i < 7; i++)
  201.         if (locations & (1 << i))
  202.             printf("%d ", i);
  203.     putchar('\n');
  204. }
  205.